<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>系统通知</h2>
        </div>
      </template>

      <el-collapse v-model="activeNames" accordion>
        <el-collapse-item
            v-for="(notice, index) in notices"
            :key="index"
            :name="index"
        >
          <template #title>
            <span class="notice-title">{{ notice.title || '系统通知' }}</span>
          </template>
          <div class="notice-meta">
            <span class="time">{{ notice.createTime }}</span>
            <span class="sender">发布人：{{ notice.sender }}</span>
          </div>
          <div class="rich-content" v-html="notice.content"></div>
        </el-collapse-item>
      </el-collapse>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import http from '@/utils/http.js'
import DOMPurify from 'dompurify'

const notices = ref([])
const activeNames = ref(null)

onMounted(() => {
  http.get('/notice/list').then(res => {
    notices.value = res.data.map(notice => ({
      ...notice,
      title: notice.title || '系统通知',
      sender: notice.sender || '系统管理员',
      content: DOMPurify.sanitize(notice.content)
    }))
  })
})
</script>

<style scoped>
.notice-meta {
  margin-bottom: 1em;
  color: #666;
  font-size: 0.9em;
}

.notice-meta .time {
  margin-right: 1.5em;
}

.el-collapse-item__header {
  font-weight: bold;
}

.el-collapse-item__content {
  padding: 0 20px 15px;
}

/* 添加富文本内容样式 */
.rich-content {
  line-height: 1.6;
}

.rich-content :deep h1 {
  font-size: 1.5em;
  margin: 1em 0;
}

.rich-content :deep h2 {
  font-size: 1.3em;
  margin: 0.8em 0;
}

.rich-content :deep p {
  margin: 0.5em 0;
}

.rich-content :deep img {
  max-width: 100%;
  height: auto;
  margin: 0.5em 0;
  border-radius: 4px;
}

.rich-content :deep a {
  color: #409eff;
  text-decoration: none;
}

.rich-content :deep ul,
.rich-content :deep ol {
  padding-left: 2em;
}

.rich-content :deep blockquote {
  border-left: 4px solid #ddd;
  padding-left: 1em;
  color: #666;
  margin: 1em 0;
}

.notice-title {
  font-weight: bold;
  margin-left: 20px;
}
</style>